package com.pentajock.docdb.utils;

import com.pentajock.docdb.Main;
import java.lang.reflect.*;
import java.io.*;
import java.net.*;

public class ResourceLoader {
	private static final Class[] parameters = new Class[]{URL.class};
	 
	public static void addFile(String s) {
		File f = new File(s);
		addFile(f);
	}
	
	public static void addFile(File f) {
		if (f.exists()) {
			try {
				addURL(f.toURI().toURL());
			}
			catch (Exception e) {
				Main.printStackTrace(e, f.getName() + " has malformed URL/URI");
			}
		}
	}
	
	public static void addURL(URL u) {	
		URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader();
		try {
			Method method = URLClassLoader.class.getDeclaredMethod("addURL",parameters);
			method.setAccessible(true);
			method.invoke(sysloader,new Object[]{u});
			Main.printDebug("Dynamically added " + u.toString() + " to classLoader");
		} 
		catch (Exception e) {
			Main.printStackTrace(e, "Could not add " + u.toString() + " to system classLoader");
		}
	}
}

